package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.dto.CheckGroupDTO;
import com.itheima.service.CheckGroupService;
import com.itheima.vo.CheckGroupVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@Api(tags = "检查组管理")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 分页查询检查组
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("findPage")
    @ApiOperation("分页查询检查组")
    public PageResult PageQuery(@RequestBody QueryPageBean queryPageBean) {
        return checkGroupService.PageQuery(queryPageBean);
    }

    /**
     * 新增检查组
     *
     * @param checkGroupDTO
     * @return
     */
    @PostMapping("add")
    @ApiOperation("新增检查组")
    public Result addCheckGroup(@RequestBody CheckGroupDTO checkGroupDTO,
                                @RequestParam(name = "checkitemIds") List<Integer> checkItemIds) {
        try {
            checkGroupService.addCheckGroup(checkGroupDTO, checkItemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }

    /**
     * 根据id查询检查组
     *
     * @param id
     * @return
     */
    @GetMapping("deleteById")
    @ApiOperation("删除检查组")
    public Result deleteCheckGroup(Integer id) {
        try {
            checkGroupService.deleteCheckGroup(id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    /**
     * 根据id查询检查组
     *
     * @param id
     * @return
     */
    @GetMapping("findById")
    @ApiOperation("根据id查询检查组")
    public Result getById(Integer id) {
        try {
            CheckGroupVO checkGroupVO = checkGroupService.getById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupVO);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 修改检查组
     *
     * @param checkGroupDTO
     * @return
     */
    @PostMapping("edit")
    @ApiOperation("修改检查组")
    public Result update(@RequestBody CheckGroupDTO checkGroupDTO,
                         @RequestParam(name = "checkitemIds") List<Integer> checkItemIds) {
        try {
            checkGroupService.update(checkGroupDTO, checkItemIds);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    /**
     * 查询所有检查组
     * @return
     */
    @GetMapping("findAll")
    @ApiOperation("查询所有检查组")
    public Result<List<CheckGroupVO>> findAll() {
        List<CheckGroupVO> checkGroupVOList = checkGroupService.findAll();
        return Result.success(checkGroupVOList);
    }

    /**
     * 根据套餐id查询检查组
     * @return
     */
    @GetMapping("findCheckGroupIdsBySetmealId")
    @ApiOperation("根据id查询套餐内包含的检查组")
    public Result<List<Integer>> findCheckGroupIdsBySetmealId(Integer setmealId){
        List<Integer> checkGroupIds = checkGroupService.findCheckGroupIdsBySetmealId(setmealId);
        return Result.success(checkGroupIds);
    }

}
